package com.softer.wxzj.common.util;

import com.github.stuxuhai.jpinyin.PinyinFormat;
import com.github.stuxuhai.jpinyin.PinyinHelper;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.entity.BaseBuildInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2019/12/6
 */

public class SortUtil {
    /**
     * 给一个没排序的集合，返回一个排序后的集合
     *
     * @param oldList 没排序的集合
     * @return 排序后的集合
     */
    public static List<BaseBuildInfo> order(List<BaseBuildInfo> oldList) {
        Collections.sort(oldList, new Comparator<BaseBuildInfo>() {
            @Override
            public int compare(BaseBuildInfo o1, BaseBuildInfo o2) {
                String firstName = o1.getBuildName().replaceAll("[^0-9]*", "");
                String secondName = o2.getBuildName().replaceAll("[^0-9]*", "");
                if (StringUtils.isEmpty(firstName) || StringUtils.isEmpty(secondName)){
                    return NumberEnum.ZERO.getNum();
                }
                return Integer.parseInt(firstName) - Integer.parseInt(secondName);
            }
        });
        List<BaseBuildInfo> newList = new ArrayList<>();
        newList.add(0, oldList.get(0));
        int n;
        for (int i = 1; i < oldList.size(); i++) {
            n = 1;
            while (compare(oldList.get(i), newList.get(i - n))) {
                if (i - n == 0) {
                    n = n + 1;
                    break;
                }
                n++;
            }
            int temp1 = i;
            BaseBuildInfo temp2 = oldList.get(i);
            for (int j = 1; j < n; j++) {
                i = i - 1;
            }
            newList.add(i, temp2);
            i = temp1;
        }
        return newList;
    }

    public static Boolean compare(BaseBuildInfo first, BaseBuildInfo second) {//进行比较两个字符串大小one大返回true否则返回false
        String one = first.getBuildName();
        String two = second.getBuildName();
        //将字符串转为字符数组
        char[] ones = one.toCharArray();
        char[] twos = two.toCharArray();
        int number;
        if (ones.length > twos.length) {
            number = twos.length;
        } else {
            number = ones.length;
        }
        for (int i = 0; i < number; i++) {
            //对于字符来说根据获取开头设置的优先级，同一优先级进行比较不同优先级直接获得大小
            int grade_one = getGrade(ones[i]);
            int grade_two = getGrade(twos[i]);
            if (grade_one == 1 || grade_two == 1) {
                return false;
            }
            if (grade_one > grade_two) {
                return true;
            } else {
                if (grade_one < grade_two) {
                    return false;
                } else {
                    //如果等级相等则在各自的等级内部继续比较
                    int one_num = ones[i];
                    int two_num = twos[i];
                    if (grade_one == 5) {
                        //当等级都为5时，即都为汉字
                        one_num = get_First_Pinyin(ones[i]);
                        two_num = get_First_Pinyin(twos[i]);
                        if (one_num < two_num) {
                            return true;
                        } else {
                            if (one_num > two_num) {
                                return false;
                            } else {
                                continue;
                            }
                        }
                    } else {
                        if (grade_one == 4) {
                            //当等级都为4时表示都为字符，但是默认.字符大于别的字符
                            if (ones[i] == '.') {
                                return true;
                            } else {
                                if (twos[i] == '.') {
                                    return false;
                                } else {
                                    continue;
                                }
                            }
                        } else {
                            //当等级为1,2,3是继续比较，且比较方法相同都是比较编码的大小
                            if (one_num < two_num) {
                                return true;
                            } else {
                                if (one_num > two_num) {
                                    return false;
                                } else {
                                    continue;
                                }
                            }
                        }
                    }

                }
            }
        }
        return true;

    }

    public static int get_First_Pinyin(char c) {
        //获取汉字字符的拼音的首字母
        String[] pinyins = PinyinHelper.convertToPinyinArray(c, PinyinFormat.WITHOUT_TONE);
        char[] result = pinyins[0].toCharArray();
        int nums = result[0];
        return nums;

    }

    public static int getGrade(char c) {//获取字符的优先级
        int i = c;
        if (i >= 19968 && i <= 40869) {
            return 5;
        } else {
            if (i >= 48 && i <= 57) {
                return 1;
            } else {
                if (i >= 97 && i <= 122) {
                    return 2;
                } else {
                    if (i >= 65 && i <= 90) {
                        return 3;
                    } else {
                        return 4;
                    }
                }
            }
        }
    }

}
